﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shop.User.Models;
using Shop.Core.Common;
using Shop.User.Services;
using Shop.User;

namespace Shop.Controllers
{
    public class UserController : Controller
    {
        // GET: User

        UsersAccess UA = new UsersAccess();
        /// <summary>
        /// 用户注册界面
        /// </summary>
        /// <returns></returns>
        public ActionResult UsersReg()
        {
            return View();
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="UI"></param>
        /// <param name="FC"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UsersReg(UsersInfo UI, FormCollection FC)
        {
            string TUPass = Request.Form["truepassword"];
            UI.UName = Request.Form["username"];
            string UName = FC["UName"];
            string UPass = FC["UPass"];
            string Email = FC["Email"];
            string Tel = FC["Tel"];
    
            if (UA.SelectUser(UName) != null)
            {
                ViewBag.TS = "用户名已注册";
                return View(UI);
            }
            if (UPass!= TUPass)
            {
                ViewBag.TS = "请保持密码与确认密码一致";
                return View(UI);
            }
            UI.UName = UName;
            UI.UPass = UPass;
            UI.Email = Email;
            UI.Tel = Tel;
            UI.IfVip = 0;
            UI.Status = 0;
            UI.Createdate = DateTime.Now;
            UA.InsertUsers(UI);
            return Redirect("UsersLogin");
        }
        /// <summary>
        /// 用户登陆界面
        /// </summary>
        /// <returns></returns>
        public ActionResult UsersLogin()
        {
            return View();
        }
        /// <summary>
        /// 验证用户登录
        /// </summary>
        /// <param name="UI"></param>
        /// <param name="IMGTXT"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UsersLogin(UsersInfo UI,string IMGTXT)
        {
            string UName = Request.Form["username"];
            string UPass = Request.Form["password"];
            string rndcodetxt = Session["rndcode"].ToString();
            if (IMGTXT == "")
            {
                ViewBag.TS = "请输入验证码!";
                return View(UI);
            }
            else if (!rndcodetxt.Equals(IMGTXT.ToLower()))
            {
                ViewBag.TS = "验证码错误!请确认后输入";
                return View(UI);
            }
            UI = UA.SelectUser(UName);
            if (UI == null)
            {
                ViewBag.TS = "用户名不存在";
                return View(UI);
            }
            if (UI.UPass!=UPass)
            {
                ViewBag.TS = "密码错误";
                return View(UI);
            }
            //保存用户状态
            UserState.SaveUserState(UI);
            return RedirectToAction("HomePage", "Home");
        }
        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>

        public ActionResult YanZhengMa()
        {
            return File(CheckCode.ProcessRequest(), "image/gif");
        }

    }
}